﻿using System;
using Gtk;
using Cairo;

namespace TwoBox
{
    class SharpApp : Window
    {
        Gdk.Pixbuf pb,pa;
        public static void Main()
        {
            Application.Init();
            new SharpApp();
            Application.Run();
        }
        public SharpApp() : base("MultiWidget")
        {
            // Gdk.Color col = new Gdk.Color(0, 0, 0);
            DeleteEvent += delegate { Application.Quit(); };
            SetDefaultSize(800, 500);
            SetPosition(WindowPosition.Center);
            BorderWidth = 1;

            try
            {
                pb = new Gdk.Pixbuf(@"..\..\..\image\main.bmp");//ck:窗口背景图片
            }
            catch
            {
                Console.WriteLine("Image not found");
                Environment.Exit(1);
            }
            Image image = new Image(pb);
            
            Label label = new Label("点击按钮变换图形");

            Button button1 = new Button("line");
            #region 设置按钮可选的颜色，用ColorButton类          
            //var button1 = new ColorButton();                    
            // Set RGBA color
            //button1.Rgba = new Gdk.RGBA()
            //{
            //    Red = 0,
            //    Green = 0,
            //    Blue = 0,
            //    Alpha = 0.2 // 20% translucent
            //};

            // Or Parse hex
            // button1.Rgba.Parse("#729FCF"); 
            #endregion
            button1.SetSizeRequest(30, 30);
            button1.Clicked += OnLine;

            Button button2 = new Button("circle");
            button2.SetSizeRequest(30, 30);
            button2.Clicked += OnCircle;

            DrawingArea darea = new DrawingArea();
            darea.SetSizeRequest(200, 200);
            darea.Drawn += OnExpose;

            try
            {
                pa = new Gdk.Pixbuf(@"..\..\..\image\pothook.bmp");//ck:
            }
            catch
            {
                Console.WriteLine("pothook.bmp not found");
                Environment.Exit(1);
            }
            Image pothook = new Image(pa);

            Fixed fix = new Fixed();
            fix.Put(image, 0, 0);//ck:添加背景
            
            fix.Put(button1, 30, 30);
            fix.Put(button2, 30, 80);
            fix.Put(label, 200, 10);
            fix.Put(darea, 100, 100);
            fix.Put(pothook, 250, 100);
            Add(fix);
            ShowAll();
            Application.Run();
        }
        public void OnLine(object sender, EventArgs args)
        {

        }
        public void OnCircle(object sender, EventArgs args)
        {
            //DrawingArea area = new DrawingArea();
            ////DrawingArea area = (DrawingArea)sender;
            //// area.Drawn += OnExpose;
            //Surface surface = new ImageSurface(Format.Argb32, 500, 500);
            //Context cr = new Context(surface);
            //cr.LineWidth = 0.5;
            //cr.Arc(100, 100, 50, 0, 2 * Math.PI);//ck:此处的圆心坐标以DrawingArea为基准
            //cr.Stroke();
        }
        void OnExpose(object o, DrawnArgs args)

        {
            if (o is DrawingArea da)
            {
               // Image image = new Image(@"..\..\..\image\main.bmp");
                Context cr = args.Cr;
                int width = da.Allocation.Width;
                int height = da.Allocation.Height;
                cr.LineWidth = 0.5;
                cr.
                cr.SetSourceRGB(0, 0, 0);//ck:设置填充颜色充当背景
                cr.Rectangle(0, 0, width, height);
                cr.Fill();

                cr.SetSourceRGB(255, 255, 255);//ck:设置颜色画圆，此层覆盖在上述矩形的上面
                cr.Arc(100, 100, 50, 0, 2 * Math.PI);//ck:此处的圆心坐标以DrawingArea为基准
                cr.StrokePreserve();

                cr.SetSourceRGB(0, 0.9, 0);//ck:设置颜色填充上面圆
                cr.Fill();

                #region 在圆上每隔30°画线
                cr.SetSourceRGB(255, 255, 255);
                cr.LineWidth = 2;
                for (double angle = 0; angle < 360; angle += 30)//注意数据类型，用int 不行
                {                  
                    double angleRad = angle / 180 * Math.PI;
                    PointD p = new PointD();
                    p.X = 100 + 50 * Math.Cos(angleRad);
                    p.Y = 100 + 50 * Math.Sin(angleRad);
                    cr.MoveTo(100, 100);
                    cr.LineTo(p);
                    cr.Stroke();
                }
                #endregion

                cr.GetTarget().Dispose();
                cr.Dispose();
            }
        }
    }
}
